package com.gc.web.controller.business;

import cn.hutool.core.util.NumberUtil;
import com.gc.business.domain.Attachment;
import com.gc.business.domain.KnowledgeBase;
import com.gc.business.domain.KnowledgeClassify;
import com.gc.business.service.IAttachmentService;
import com.gc.business.service.IKnowledgeBaseService;
import com.gc.business.service.IKnowledgeClassifyService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 知识库数据 控制层
 *
 * @author liujinteng
 * @date 2020-10-27
 */
@Slf4j
@Controller
@RequestMapping("/business/knowledgeBase")
public class KnowledgeBaseController extends BaseController {
    private String prefix = "business/knowledgeBase";

    @Autowired
    private ISysUserService userService;

    private final IKnowledgeBaseService knowledgeBaseService;

    private final IKnowledgeClassifyService knowledgeClassifyService;

    private final IAttachmentService attachmentService;

    @Autowired
    public KnowledgeBaseController(IKnowledgeBaseService knowledgeBaseService,
                                   IKnowledgeClassifyService knowledgeClassifyService,
                                   IAttachmentService attachmentService) {
        this.knowledgeBaseService = knowledgeBaseService;
        this.knowledgeClassifyService = knowledgeClassifyService;
        this.attachmentService = attachmentService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:knowledgeBase:view")
    @GetMapping
    public String knowledgeBase() {
        return prefix + "/knowledgeBase";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        KnowledgeClassify knowledgeClassify = new KnowledgeClassify();
        knowledgeClassify.setAttrId(getAttrId());
        List<KnowledgeClassify> knowledgeClassifies = knowledgeClassifyService.list(knowledgeClassify);
        modelMap.put("knowledgeClassifies",knowledgeClassifies);
        return prefix + "/add";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{baseId}")
    public String edit(@PathVariable("baseId") Long baseId, ModelMap mmap) {
        KnowledgeBase knowledgeBase = knowledgeBaseService.getById(baseId);
        List<Attachment> attachmentList = new ArrayList<>();
        if (StringUtils.isNotEmpty(knowledgeBase.getVideoUrl())){
            if(knowledgeBase.getVideoUrl().contains(",")){
                String[] split = knowledgeBase.getVideoUrl().split(",");
                for (String attachmentId : split) {
                    Attachment attachment = attachmentService.getById(Long.valueOf(attachmentId));
                    if(StringUtils.isNotNull(attachment)){
                        attachmentList.add(attachment);
                    }
                }
            }else {
                if(!NumberUtil.isNumber(knowledgeBase.getVideoUrl())){
                    return "error/404";
                }
                Attachment attachment = attachmentService.getById(Long.valueOf(knowledgeBase.getVideoUrl()));
                if(StringUtils.isNotNull(attachment)){
                    attachmentList.add(attachment);
                }
            }
            mmap.put("attachmentList",attachmentList);
        }
        List<KnowledgeClassify> knowledgeClassifies = knowledgeClassifyService.list(new KnowledgeClassify());
        mmap.put("knowledgeClassifies",knowledgeClassifies);
        mmap.put("knowledgeBase", knowledgeBase);
        return prefix + "/edit";
    }

    /**
     * 查询知识库数据列表
     */
    @RequiresPermissions("business:knowledgeBase:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(KnowledgeBase knowledgeBase) {
        Long attrId = getAttrId();
        Boolean ifCompany = userService.ifCompanyRole(attrId);
        if(!ifCompany){
            return Result.success(getDataTable(new ArrayList<KnowledgeBase>()));
        }
        startPage();
        knowledgeBase.setAttrId(attrId);
        List<KnowledgeBase> list = knowledgeBaseService.list(knowledgeBase);
        return Result.success(getDataTable(list));
    }

    /**
     * 新增保存知识库数据
     */
    @RequiresPermissions("business:knowledgeBase:add")
    @Log(title = "知识库数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(KnowledgeBase knowledgeBase) {
        if (knowledgeBase.getClassifyId() == null){
            return  Result.failed("参数错误:分类ID不存在");
        }
        KnowledgeClassify knowledgeClassify = knowledgeClassifyService.getById(knowledgeBase.getClassifyId());
        knowledgeBase.setAttrId(getAttrId());
        knowledgeBase.setClassifyName(knowledgeClassify.getClassifyName());
        knowledgeBaseService.save(knowledgeBase);
        return Result.success();
    }

    /**
     * 修改保存知识库数据
     */
    @RequiresPermissions("business:knowledgeBase:edit")
    @Log(title = "知识库数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(KnowledgeBase knowledgeBase) {
        knowledgeBaseService.updateById(knowledgeBase);
        return Result.success();
    }

    /**
     * 删除知识库数据
     */
    @RequiresPermissions("business:knowledgeBase:remove")
    @Log(title = "知识库数据", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        knowledgeBaseService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出知识库数据列表
     */
    @RequiresPermissions("business:knowledgeBase:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(KnowledgeBase knowledgeBase) {
        List<KnowledgeBase> list = knowledgeBaseService.list(knowledgeBase);
        ExcelUtil<KnowledgeBase> util = new ExcelUtil<>(KnowledgeBase.class);
        util.exportExcel(list, "knowledgeBase");
        return Result.success();
    }
}
